<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>JS基础之数据类型及其判断 | 小飞侠Pan的Blog</title><meta name="keywords" content="JS,数据类型"><meta name="author" content="潘静怡"><meta name="copyright" content="潘静怡"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一.JS 中一共有八种数据类型 基本数据类型（值类型）：String 字符串、Number 数值、BigInt 大型数值、Boolean 布尔值、Null 空值、Undefined 未定义、Symbol。 引用数据类型（引用类型）：Object 对象。  注意：内置对象 Function、Array、Date、RegExp、Error 等都是属于 Object 类型。也就是说，除了那七种基本数据类">
<meta property="og:type" content="article">
<meta property="og:title" content="JS基础之数据类型及其判断">
<meta property="og:url" content="https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/index.html">
<meta property="og:site_name" content="小飞侠Pan的Blog">
<meta property="og:description" content="一.JS 中一共有八种数据类型 基本数据类型（值类型）：String 字符串、Number 数值、BigInt 大型数值、Boolean 布尔值、Null 空值、Undefined 未定义、Symbol。 引用数据类型（引用类型）：Object 对象。  注意：内置对象 Function、Array、Date、RegExp、Error 等都是属于 Object 类型。也就是说，除了那七种基本数据类">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg">
<meta property="article:published_time" content="2022-02-26T10:49:21.438Z">
<meta property="article:modified_time" content="2022-02-26T10:59:12.393Z">
<meta property="article:author" content="潘静怡">
<meta property="article:tag" content="JS">
<meta property="article:tag" content="数据类型">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg"><link rel="shortcut icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg"><link rel="canonical" href="https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":500},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JS基础之数据类型及其判断',
  isPost: true,
  isHome: false,
  isHighlightShrink: undefined,
  isToc: true,
  postUpdate: '2022-02-26 18:59:12'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/social.css" type="text/css"><link rel="stylesheet" href="/css/style.css" type="text/css"><meta name="generator" content="Hexo 5.4.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/book/"><i class="fa-fw fa-solid fa-bookmark"></i><span> 前端知识库</span></a></div><div class="menus_item"><a class="site-page" href="/work/"><i class="fa-fw fa fa-heartbeat"></i><span> 在线简历</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa-solid fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小飞侠Pan的Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/book/"><i class="fa-fw fa-solid fa-bookmark"></i><span> 前端知识库</span></a></div><div class="menus_item"><a class="site-page" href="/work/"><i class="fa-fw fa fa-heartbeat"></i><span> 在线简历</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa-solid fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">JS基础之数据类型及其判断</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-26T10:49:21.438Z" title="发表于 2022-02-26 18:49:21">2022-02-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-26T10:59:12.393Z" title="更新于 2022-02-26 18:59:12">2022-02-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JS/">JS</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">6.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>25分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JS基础之数据类型及其判断"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div><article class="post-content" id="article-container"><h3 id="一-JS-中一共有八种数据类型"><a href="#一-JS-中一共有八种数据类型" class="headerlink" title="一.JS 中一共有八种数据类型"></a>一.JS 中一共有八种数据类型</h3><ul>
<li><strong>基本数据类型（值类型）</strong>：String 字符串、Number 数值、BigInt 大型数值、Boolean 布尔值、Null 空值、Undefined 未定义、Symbol。</li>
<li><strong>引用数据类型（引用类型）</strong>：Object 对象。</li>
</ul>
<p>注意：内置对象 Function、Array、Date、RegExp、Error 等都是属于 Object 类型。也就是说，除了那七种基本数据类型之外，其他的，都称之为 Object 类型。</p>
<p>BigInt 和 Symbol 是ES6中新增的类型。</p>
<h3 id="二-栈内存和堆内存"><a href="#二-栈内存和堆内存" class="headerlink" title="二.栈内存和堆内存"></a>二.栈内存和堆内存</h3><p>JS 中，所有的<strong>变量</strong>都是保存在<strong>栈内存</strong>中的。</p>
<p><strong>基本数据类型</strong>：</p>
<p>基本数据类型的值，直接保存在栈内存中。值与值之间是独立存在，修改一个变量不会影响其他的变量。</p>
<p><strong>引用数据类型</strong>：</p>
<p>对象是保存到<strong>堆内存</strong>中的。每创建一个新的对象，就会在堆内存中开辟出一个新的空间；而<strong>变量保存了对象的内存地址</strong>（对象的引用），保存在栈内存当中。如果两个变量保存了同一个对象的引用，当一个通过一个变量修改属性时，另一个也会受到影响。</p>
<h3 id="三-基本数据类型：Null-和-Undefined"><a href="#三-基本数据类型：Null-和-Undefined" class="headerlink" title="三.基本数据类型：Null 和 Undefined"></a>三.基本数据类型：Null 和 Undefined</h3><h4 id="Null：空对象"><a href="#Null：空对象" class="headerlink" title="Null：空对象"></a>Null：空对象</h4><p>null 专门用来定义一个<strong>空对象</strong>。例如：<code>let a = null</code>，又例如 <code>Object.create(null)</code>.</p>
<p>如果你想定义一个变量用来保存引用类型，但是还没想好放什么内容，这个时候，可以在初始化时将其设置为 null。你可以把 null 理解为：<strong>null 虽然是一个单独的数据类型，但null 相当于是一个 object，只不过地址为空（空指针）而已</strong>。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> myObj = <span class="literal">null</span>;</span><br><span class="line">cosole.<span class="title function_">log</span>(<span class="keyword">typeof</span> myObj); <span class="comment">// 打印结果：object</span></span><br></pre></td></tr></table></figure>

<ul>
<li>Null 类型的值只有一个，就是 null。比如 <code>let a = null</code>。</li>
<li>使用 typeof 检查一个 null 值时，会返回 object。</li>
</ul>
<h4 id="undefined：未定义类型"><a href="#undefined：未定义类型" class="headerlink" title="undefined：未定义类型"></a>undefined：未定义类型</h4><h6 id="case1：变量已声明，未赋值时"><a href="#case1：变量已声明，未赋值时" class="headerlink" title="case1：变量已声明，未赋值时"></a>case1：变量已声明，未赋值时</h6><p><strong>声明</strong>了一个变量，但没有<strong>赋值</strong>，此时它的值就是 <code>undefined</code>。</p>
<p>举例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name); <span class="comment">// 打印结果：undefined</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> name); <span class="comment">// 打印结果：undefined</span></span><br></pre></td></tr></table></figure>

<ul>
<li>Undefined 类型的值只有一个，就是 undefind。比如 <code>let a = undefined</code>。</li>
<li>使用 typeof 检查一个 undefined 值时，会返回 undefined。</li>
</ul>
<h6 id="case2：变量未声明（未定义）时"><a href="#case2：变量未声明（未定义）时" class="headerlink" title="case2：变量未声明（未定义）时"></a>case2：变量未声明（未定义）时</h6><p>如果你从未声明一个变量，就去使用它，则会报错；此时，如果用 <code>typeof</code> 检查这个变量时，会返回 <code>undefined</code>。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// undefined</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// 打印结果：Uncaught ReferenceError: a is not defined</span></span><br></pre></td></tr></table></figure>

<h6 id="case3：函数无返回值时"><a href="#case3：函数无返回值时" class="headerlink" title="case3：函数无返回值时"></a>case3：函数无返回值时</h6><p>如果一个函数没有返回值，那么，这个函数的返回值就是 undefined。</p>
<p>在定义一个函数时，如果末尾没有 return 语句，那么，其实就是 <code>return undefined</code>。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">foo</span>()); <span class="comment">// 打印结果：undefined</span></span><br></pre></td></tr></table></figure>

<h6 id="case4：调用函数时，未传参"><a href="#case4：调用函数时，未传参" class="headerlink" title="case4：调用函数时，未传参"></a>case4：调用函数时，未传参</h6><p>调用函数时，如果没有传参，那么，这个参数的值就是 undefined。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">foo</span>(); <span class="comment">// 调用函数时，未传参。执行函数后的打印结果：undefined</span></span><br></pre></td></tr></table></figure>

<p>实际开发中，如果调用函数时没有传参，我们可以根据需要给形参设置一个默认值：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    name = name || <span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">foo</span>();</span><br></pre></td></tr></table></figure>

<p> ES6 的方式：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params">name = <span class="string">&#x27;xioafeixiaPan&#x27;</span></span>) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">foo</span>();</span><br></pre></td></tr></table></figure>

<h4 id="Null-和-Undefined的区别"><a href="#Null-和-Undefined的区别" class="headerlink" title="Null 和 Undefined的区别"></a>Null 和 Undefined的区别</h4><p> <code>null == undefined</code> 的结果为 <code>true</code>，但是 <code>null === undefined</code> 的结果是 false。</p>
<ul>
<li>10 + null 结果为 10。</li>
<li>10 + undefined 结果为 NaN。</li>
</ul>
<p>规律总结：</p>
<ul>
<li>任何值和 null 运算，null 可看做 0 运算。</li>
<li>任何数据类型和 undefined 运算都是 NaN。</li>
</ul>
<h3 id="四-基本数据类型：Number"><a href="#四-基本数据类型：Number" class="headerlink" title="四.基本数据类型：Number"></a>四.基本数据类型：Number</h3><h4 id="数值范围"><a href="#数值范围" class="headerlink" title="数值范围"></a>数值范围</h4><p>由于内存的限制，ECMAScript 并不能保存世界上所有的数值。</p>
<ul>
<li>最大值：<code>Number.MAX_VALUE</code>，这个值为： 1.7976931348623157e+308</li>
<li>最小值：<code>Number.MIN_VALUE</code>，这个值为： 5e-324</li>
</ul>
<p>如果使用 Number 表示的变量超过了最大值，则会返回 Infinity。</p>
<ul>
<li>无穷大（正无穷）：Infinity</li>
<li>无穷小（负无穷）：-Infinity</li>
</ul>
<p>注意：<code>typeof Infinity</code>的返回结果是 number。</p>
<p>如果分子是非0值，分母是有符号0或无符号0，则会返回<code>Infinity</code>或<code>-Infinity</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">5</span>/<span class="number">0</span>);<span class="comment">//Infinity</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">5</span>/-<span class="number">0</span>);<span class="comment">//-Infinity</span></span><br></pre></td></tr></table></figure>



<h4 id="NaN"><a href="#NaN" class="headerlink" title="NaN"></a>NaN</h4><p><strong>NaN</strong>：是一个特殊的数字，表示 Not a Number，非数值。比如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;abc&#x27;</span> / <span class="number">18</span>); <span class="comment">//结果是NaN</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;abc&#x27;</span> * <span class="string">&#x27;abcd&#x27;</span>); <span class="comment">//NaN</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0</span>/<span class="number">0</span>);<span class="comment">//NaN</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0</span>/-<span class="number">0</span>);<span class="comment">//NaN</span></span><br></pre></td></tr></table></figure>

<p>注意：</p>
<ol>
<li><p><code>typeof NaN</code>的返回结果是 number。</p>
</li>
<li><p><strong>Undefined 和任何数值计算的结果为 NaN。NaN 与任何值都不相等，包括 NaN 本身。</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">NaN</span>==<span class="title class_">NaN</span>);<span class="comment">//false</span></span><br></pre></td></tr></table></figure></li>
<li><p>任何涉及NaN的操作始终返回NaN(如NaN/10)</p>
</li>
</ol>
<p><strong>isNaN()函数</strong>：</p>
<p>ECMAScript 提供了isNaN()函数。该函数接收一个参数，可以是任意数据类型，然后判断<br>这个参数是否“不是数值”。把一个值传给isNaN()后，该函数会尝试把它转换为数值。某些非数值的<br>值可以直接转换成数值，如字符串”10”或布尔值。任何不能转换为数值的值都会导致这个函数返回<br>true。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="title class_">NaN</span>)); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="number">10</span>)); <span class="comment">// false，10 是数值</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="string">&quot;10&quot;</span>)); <span class="comment">// false，可以转换为数值10</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="string">&quot;blue&quot;</span>)); <span class="comment">// true，不可以转换为数值</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="literal">true</span>)); <span class="comment">// false，可以转换为数值1</span></span><br></pre></td></tr></table></figure>

<h6 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h6><p>Number()、parseInt()、parseFloat()</p>
<p><strong>Number</strong></p>
<p>使用Number函数基本上可以转换所有类型</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">console.log(Number(&#x27;xiaofeixiaPan&#x27;)); //NaN</span><br><span class="line">console.log(Number(true));	//1</span><br><span class="line">console.log(Number(false));	//0</span><br><span class="line">console.log(Number(&#x27;9&#x27;));	//9</span><br><span class="line">console.log(Number([]));	//0</span><br><span class="line">console.log(Number([5]));	//5</span><br><span class="line">console.log(Number([5, 2]));	//NaN</span><br><span class="line">console.log(Number(&#123;&#125;));	//NaN</span><br></pre></td></tr></table></figure>

<p>Number()函数基于如下规则执行转换。<br> 布尔值，true 转换为1，false 转换为0。<br> 数值，直接返回。<br> null，返回0。<br> undefined，返回NaN。<br> 字符串，应用以下规则。<br>             如果字符串包含数值字符，包括数值字符前面带加、减号的情况，则转换为一个十进制数值。因此，Number(“1”)返回1，Number(“123”)返回123，Number(“011”)返回11（忽略前面<br>的零）。<br>        如果字符串包含有效的浮点值格式如”1.1”，则会转换为相应的浮点值（同样，忽略前面的零）。<br>        如果字符串包含有效的十六进制格式如”0xf”，则会转换为与该十六进制值对应的十进制整<br>数值。<br>         如果是空字符串（不包含字符），则返回0。<br>        如果字符串包含除上述情况之外的其他字符，则返回NaN。<br> 对象，调用valueOf()方法，并按照上述规则转换返回的值。如果转换结果是NaN，则调用<br>toString()方法，再按照转换字符串的规则转换。</p>
<p><strong>parseInt</strong></p>
<p>空字符串也会返回NaN（这一点跟Number()不一样，它返回0）</p>
<p>parseInt()函数也能识别不同的整数格式（十进制、八进制、十六进制）。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(<span class="string">&#x27;  99xioafeixiaPan&#x27;</span>));	<span class="comment">//99</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(<span class="string">&#x27;18.55&#x27;</span>));	<span class="comment">//18</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num1 = <span class="built_in">parseInt</span>(<span class="string">&quot;1234blue&quot;</span>); <span class="comment">// 1234</span></span><br><span class="line"><span class="keyword">let</span> num2 = <span class="built_in">parseInt</span>(<span class="string">&quot;&quot;</span>); <span class="comment">// NaN</span></span><br><span class="line"><span class="keyword">let</span> num3 = <span class="built_in">parseInt</span>(<span class="string">&quot;0xA&quot;</span>); <span class="comment">// 10，解释为十六进制整数</span></span><br><span class="line"><span class="keyword">let</span> num4 = <span class="built_in">parseInt</span>(<span class="number">22.5</span>); <span class="comment">// 22</span></span><br><span class="line"><span class="keyword">let</span> num5 = <span class="built_in">parseInt</span>(<span class="string">&quot;70&quot;</span>); <span class="comment">// 70，解释为十进制值</span></span><br><span class="line"><span class="keyword">let</span> num6 = <span class="built_in">parseInt</span>(<span class="string">&quot;0xf&quot;</span>); <span class="comment">// 15，解释为十六进制整数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//parseInt()也接收第二个参数</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="built_in">parseInt</span>(<span class="string">&quot;0xAF&quot;</span>, <span class="number">16</span>); <span class="comment">// 175</span></span><br></pre></td></tr></table></figure>



<p><strong>parseFloat</strong></p>
<p>parseFloat()只解析十进制值,十六进制数值始终会返回0。</p>
<p>第一次出现的小数点是有效的，但第二次出现的小数点就无效了，此时字符串的剩余字符都会被忽略。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num1 = <span class="built_in">parseFloat</span>(<span class="string">&quot;1234blue&quot;</span>); <span class="comment">// 1234，按整数解析</span></span><br><span class="line"><span class="keyword">let</span> num2 = <span class="built_in">parseFloat</span>(<span class="string">&quot;0xA&quot;</span>); <span class="comment">// 0</span></span><br><span class="line"><span class="keyword">let</span> num3 = <span class="built_in">parseFloat</span>(<span class="string">&quot;22.5&quot;</span>); <span class="comment">// 22.5</span></span><br><span class="line"><span class="keyword">let</span> num4 = <span class="built_in">parseFloat</span>(<span class="string">&quot;22.34.5&quot;</span>); <span class="comment">// 22.34</span></span><br><span class="line"><span class="keyword">let</span> num5 = <span class="built_in">parseFloat</span>(<span class="string">&quot;0908.5&quot;</span>); <span class="comment">// 908.5</span></span><br><span class="line"><span class="keyword">let</span> num6 = <span class="built_in">parseFloat</span>(<span class="string">&quot;3.125e7&quot;</span>); <span class="comment">// 31250000</span></span><br></pre></td></tr></table></figure>

<h6 id="舍入操作"><a href="#舍入操作" class="headerlink" title="舍入操作"></a>舍入操作</h6><p>使用 <code>toFixed</code> 可对数值舍入操作，参数指定保存的小数位</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console.log(1.556.toFixed(2)); //1.56</span><br></pre></td></tr></table></figure>

<h6 id="方式（隐式的类型转换）："><a href="#方式（隐式的类型转换）：" class="headerlink" title="方式（隐式的类型转换）："></a>方式（隐式的类型转换）：</h6><p> 使用一元的+来进行隐式的类型转换<br> 例子：  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;123&quot;</span>;  </span><br><span class="line">a = +a;  </span><br></pre></td></tr></table></figure>

<p> <strong>原理：和Number()函数一样</strong> </p>
<h3 id="五-基本数据类型：Boolean"><a href="#五-基本数据类型：Boolean" class="headerlink" title="五.基本数据类型：Boolean"></a>五.基本数据类型：Boolean</h3><h6 id="隐式转换"><a href="#隐式转换" class="headerlink" title="隐式转换"></a>隐式转换</h6><p>基本上所有类型都可以隐式转换为 Boolean类型。</p>
<table>
<thead>
<tr>
<th>数据类型</th>
<th>true</th>
<th>false</th>
</tr>
</thead>
<tbody><tr>
<td>String</td>
<td>非空字符串</td>
<td>空字符串</td>
</tr>
<tr>
<td>Number</td>
<td>非0的数值</td>
<td>0 、NaN</td>
</tr>
<tr>
<td>Array</td>
<td>数组不参与比较时</td>
<td>参与比较的空数组</td>
</tr>
<tr>
<td>Object</td>
<td>所有对象</td>
<td>null</td>
</tr>
<tr>
<td>undefined</td>
<td>无</td>
<td>undefined</td>
</tr>
<tr>
<td>null</td>
<td>无</td>
<td>null</td>
</tr>
<tr>
<td>NaN</td>
<td>无</td>
<td>NaN</td>
</tr>
</tbody></table>
<p>在JavaScript中，布尔值为 <code>false</code>的值有如下几个：</p>
<ul>
<li><code>undefined</code></li>
<li><code>null</code></li>
<li><code>false</code></li>
<li><code>0</code></li>
<li><code>NaN</code></li>
<li><code>&quot;&quot;</code>或<code>&#39;&#39;</code>（空字符串）</li>
</ul>
<p>当与boolean类型比较时，会将两边类型统一为数字1或0。</p>
<p>如果使用Boolean与数值比较时，会进行隐式类型转换 true转为1，false 转为0。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">3</span> == <span class="literal">true</span>); <span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0</span> == <span class="literal">false</span>); <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p>字符串在与Boolean比较时，两边都为转换为数值类型后再进行比较。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;1&quot;</span> == <span class="literal">true</span>); <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;houdunren&quot;</span> == <span class="literal">true</span>); <span class="comment">//false</span></span><br><span class="line"><span class="comment">//Number(&quot;houdunren&quot;)为NaN</span></span><br></pre></td></tr></table></figure>

<h6 id="显式转换"><a href="#显式转换" class="headerlink" title="显式转换"></a>显式转换</h6><p>（1）使用 <code>!!</code> 转换布尔类型</p>
<p><code>!!</code>的作用是将值的类型强制转化为布尔类型。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span>==<span class="literal">false</span>);<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span>==<span class="literal">false</span>);<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">NaN</span>==<span class="literal">false</span>);<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;&#x27;</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;-------------------------&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(!!<span class="literal">undefined</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(!!<span class="literal">null</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(!!<span class="number">0</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(!!<span class="title class_">NaN</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(!!<span class="string">&#x27;&#x27;</span>==<span class="literal">false</span>);<span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p><strong>返回值强制转换</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">test</span>=value=&gt; value &amp;&amp; <span class="literal">true</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">test</span>(<span class="number">0</span>)); <span class="comment">// 0</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">test</span>=value=&gt; !!value &amp;&amp; <span class="literal">true</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">test</span>(<span class="number">0</span>)); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>可以看到，<code>!!</code>把 <code>value</code>强制转化为布尔值。</p>
<p>（2）使用 <code>Boolean</code> 函数可以显式转换为布尔类型</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> hd = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Boolean</span>(hd)); <span class="comment">//false</span></span><br><span class="line">hd = <span class="number">0</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Boolean</span>(hd)); <span class="comment">//false</span></span><br><span class="line">hd = <span class="literal">null</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Boolean</span>(hd)); <span class="comment">//false</span></span><br><span class="line">hd = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="string">&quot;2020-2-22 10:33&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Boolean</span>(hd)); <span class="comment">//true</span></span><br></pre></td></tr></table></figure>



<h3 id="六-0-“”-null-false-undefined的区别"><a href="#六-0-“”-null-false-undefined的区别" class="headerlink" title="六.0, “”, null, false, undefined的区别"></a>六.0, “”, null, false, undefined的区别</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">false</span>==<span class="literal">undefined</span>);	<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">false</span>==<span class="literal">null</span>);		<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span>==<span class="number">0</span>);		<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span>==<span class="number">0</span>);		<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;&quot;</span>==<span class="number">0</span>);			<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">false</span>==<span class="number">0</span>);		<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">false</span>==<span class="string">&quot;&quot;</span>);		<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span>==<span class="literal">undefined</span>);	<span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p>观察可发现：0、””和false是相等的，null和undefined是相等的，而undefined和null并不等于false对象。<br>可以把0、””和false归为一类，称之为“假值”，把null和undefined归为一类，称之为“空值”。假值还是一个有效的对象，所以可以对其使用toString等类型相关方法，空值则不行。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">false</span>.<span class="title function_">toString</span>());    <span class="comment">// false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;&quot;</span>.<span class="property">length</span>);         <span class="comment">// 0</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>((<span class="number">0</span>).<span class="title function_">toString</span>());   <span class="comment">// 0</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span>.<span class="title function_">toString</span>());   </span><br><span class="line"><span class="comment">// throw exception &quot;undefined has no properties&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span>.<span class="title function_">toString</span>());        <span class="comment">// &quot;null has no properties&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="七-基本数据类型：String"><a href="#七-基本数据类型：String" class="headerlink" title="七.基本数据类型：String"></a>七.基本数据类型：String</h3><h4 id="转换为String"><a href="#转换为String" class="headerlink" title="转换为String"></a>转换为String</h4><h6 id="方式一（强制类型转换）："><a href="#方式一（强制类型转换）：" class="headerlink" title="方式一（强制类型转换）："></a>方式一（强制类型转换）：</h6><p> <strong>调用被转换数据的toString()方法</strong><br> 例子：<br>var a = 123;<br>a = a.toString();<br> 注意：<strong>这个方法不适用于null和undefined</strong><br>由于这两个类型的数据中没有方法，所以调用toString()时会报错  </p>
<h6 id="方式二（强制类型转换）："><a href="#方式二（强制类型转换）：" class="headerlink" title="方式二（强制类型转换）："></a>方式二（强制类型转换）：</h6><p> <strong>调用String()函数</strong><br> 例子：  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">123</span>;  </span><br><span class="line">a = <span class="title class_">String</span>(a);  </span><br></pre></td></tr></table></figure>

<p> 原理：<strong>对于Number Boolean String都会调用他们的toString()方法来将其转换为字符串，对于null值，直接转换为字符串”null”。对于undefined直接转换为字符串”undefined”</strong>  </p>
<h4 id="方式三（隐式的类型转换）"><a href="#方式三（隐式的类型转换）" class="headerlink" title="方式三（隐式的类型转换）:"></a>方式三（隐式的类型转换）:</h4><p> <strong>为任意的数据类型 +””</strong><br> 例子：  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="literal">true</span>;  </span><br><span class="line">a = a + <span class="string">&quot;&quot;</span>; </span><br></pre></td></tr></table></figure>

<p> 原理：和String()函数一样      </p>
<p><strong>语法</strong>:</p>
<p>字符串型可以是引号中的任意文本，其语法为：双引号 <code>&quot;&quot;</code> 或者单引号 <code>&#39;&#39;</code>。</p>
<p><strong>字符串的不可变性</strong>:</p>
<p>字符串里面的值不可被改变。虽然看上去可以改变内容，但其实是地址变了，内存中新开辟了一个内存空间。</p>
<p>代码举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;hello&#x27;</span>;</span><br><span class="line"></span><br><span class="line">str = <span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>比如上面的代码，当重新给变量 str 赋值时，常量<code>hello</code>不会被修改，依然保存在内存中；str 会改为指向<code>xiaofeixiaPan</code>。</p>
<h4 id="字符串的常用方法"><a href="#字符串的常用方法" class="headerlink" title="字符串的常用方法"></a>字符串的常用方法</h4><p><strong>字符串的所有方法，都不会改变原字符串</strong>（字符串的不可变性），操作完成后会返回一个新的值。</p>
<p>我们也可将字符串常用的操作方法归纳为增、删、改、查，需要知道字符串的特点是一旦创建了，就不可变。</p>
<h6 id="增："><a href="#增：" class="headerlink" title="增："></a>增：</h6><p><strong>concat</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> stringValue = <span class="string">&quot;hello &quot;</span>;</span><br><span class="line"><span class="keyword">let</span> result = stringValue.<span class="title function_">concat</span>(<span class="string">&quot;world&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">// &quot;hello world&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(stringValue); <span class="comment">// &quot;hello&quot;</span></span><br></pre></td></tr></table></figure>

<h6 id="删："><a href="#删：" class="headerlink" title="删："></a>删：</h6><p>常见的有：</p>
<ul>
<li>slice()</li>
<li>substr()</li>
<li>substring()</li>
</ul>
<p>都是字符串截取。</p>
<p>（1）slice( )</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新字符串 = str.<span class="title function_">slice</span>(开始索引, 结束索引); <span class="comment">//两个参数都是索引值。包左不包右。</span></span><br></pre></td></tr></table></figure>

<p>解释：从字符串中截取指定的内容。不会修改原字符串，而是将截取到的内容返回。</p>
<p>注意：上面的参数，包左不包右。参数举例如下：</p>
<ul>
<li><p><code>(2, 5)</code> 截取时，包左不包右。</p>
</li>
<li><p><code>(2)</code> 表示<strong>从指定的索引位置开始，截取到最后</strong>。</p>
</li>
<li><p><code>(-3)</code> 表示从倒数第三个开始，截取到最后。</p>
</li>
<li><p><code>(1, -1)</code> 表示从第一个截取到倒数第一个。</p>
</li>
<li><p><code>(5, 2)</code> 表示前面的大，后面的小，返回值为空。</p>
</li>
</ul>
<p>​    (2).substring( )</p>
<p>语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新字符串 = str.<span class="title function_">substring</span>(开始索引, 结束索引); <span class="comment">//两个参数都是索引值。包左不包右。</span></span><br></pre></td></tr></table></figure>

<p>解释：从字符串中截取指定的内容。和<code>slice()</code>类似。</p>
<p><code>substring()</code>和<code>slice()</code>是类似的。但不同之处在于：</p>
<ul>
<li><p><code>substring()</code>不能接受负值作为参数。如果传递了一个<strong>负值</strong>，则默认使用 0。</p>
</li>
<li><p><code>substring()</code>还会自动调整参数的位置，如果第二个参数小于第一个，则自动交换。比如说， <code>substring(1, 0)</code>相当于截取的是第一个字符。</p>
</li>
</ul>
<p>​    (3).substr( )</p>
<p>语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符串 = str.<span class="title function_">substr</span>(开始索引, 截取的长度);</span><br></pre></td></tr></table></figure>

<p>解释：从字符串中截取指定的内容。不会修改原字符串，而是将截取到的内容返回。</p>
<p>注意，这个方法的第二个参数<strong>截取的长度</strong>，不是结束索引。</p>
<p>参数举例：</p>
<ul>
<li><p><code>(2,4)</code> 从索引值为 2 的字符开始，截取 4 个字符。</p>
</li>
<li><p><code>(1)</code> 从指定位置开始，截取到最后。</p>
</li>
<li><p><code>(-3)</code> 从倒数第几个开始，截取到最后。</p>
</li>
</ul>
<p>备注：ECMAscript 没有对 <code>substr()</code> 方法进行标准化，因此不建议使用它。</p>
<h6 id="改："><a href="#改：" class="headerlink" title="改："></a>改：</h6><p>常见的有：</p>
<ul>
<li>trim()、trimLeft()、trimRight()</li>
<li>repeat()</li>
<li>padStart()、padEnd()</li>
<li>toLowerCase()、 toUpperCase()</li>
<li>replace()</li>
</ul>
<p>（1）<code>trim()</code>：去除字符串前后的空白。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//去除字符串前后的空格，trim();</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;   a   b   c   &#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str);<span class="comment">//   a   b   c   </span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="property">length</span>);<span class="comment">//15</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trim</span>());<span class="comment">//a   b   c</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trim</span>().<span class="property">length</span>);<span class="comment">//9</span></span><br></pre></td></tr></table></figure>

<p>使用<code>trimLeft</code>删除左边空白，使用<code>trimRight</code>删除右边空白</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="string">&quot; xiaofeixiaPan &quot;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">trimLeft</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">trimRight</span>()); </span><br></pre></td></tr></table></figure>

<p>（2）大小写转换:toLowerCase()、toUpperCase()</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;abcdEFG&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//转换成小写</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">toLowerCase</span>());<span class="comment">//abcdefg</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//转换成大写</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">toUpperCase</span>());<span class="comment">//ABCDEFG</span></span><br></pre></td></tr></table></figure>

<p>(3) repeat()：重复字符串</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">newStr = str.<span class="title function_">repeat</span>(重复的次数);</span><br></pre></td></tr></table></figure>

<p>解释：将字符串重复指定的次数。会返回新的值，不会修改原字符串。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">repeat</span>(<span class="number">2</span>));<span class="comment">//xiaofeixiaPanxiaofeixiaPan</span></span><br></pre></td></tr></table></figure>

<p>举例2：（模糊字符串的后四位）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> telephone = <span class="string">&#x27;13088889999&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> mix_telephone = telephone.<span class="title function_">slice</span>(<span class="number">0</span>, -<span class="number">4</span>) + <span class="string">&#x27;*&#x27;</span>.<span class="title function_">repeat</span>(<span class="number">4</span>); <span class="comment">// 模糊电话号码的后四位</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(telephone); <span class="comment">// 打印结果：13088889999</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(mix_telephone); <span class="comment">// 打印结果：1308888****</span></span><br></pre></td></tr></table></figure>

<p>（4）padEnd()</p>
<p>复制字符串，如果小于指定长度，则在相应一边填充字符，直至满足长度条件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> stringValue = <span class="string">&quot;foo&quot;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(stringValue.<span class="title function_">padStart</span>(<span class="number">6</span>)); <span class="comment">// &quot; foo&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(stringValue.<span class="title function_">padStart</span>(<span class="number">9</span>, <span class="string">&quot;.&quot;</span>)); <span class="comment">// &quot;......foo&quot;</span></span><br></pre></td></tr></table></figure>

<p>(5) replace()</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新的字符串 = str.<span class="title function_">replace</span>(被替换的字符，新的字符);</span><br></pre></td></tr></table></figure>

<p>解释：将字符串中的指定内容，替换为新的内容并返回。不会修改原字符串。</p>
<p>注意：这个方法，默认只会替换第一个被匹配到的字符。如果要全局替换，需要使用正则。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//replace()方法：替换</span></span><br><span class="line"><span class="keyword">var</span> str2 = <span class="string">&#x27;Today is fine day,today is fine day !&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str2);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str2.<span class="title function_">replace</span>(<span class="string">&#x27;today&#x27;</span>, <span class="string">&#x27;tomorrow&#x27;</span>)); <span class="comment">//只能替换第一个today</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str2.<span class="title function_">replace</span>(<span class="regexp">/today/gi</span>, <span class="string">&#x27;tomorrow&#x27;</span>)); <span class="comment">//这里用到了正则，才能替换所有的today</span></span><br></pre></td></tr></table></figure>

<h6 id=""><a href="#" class="headerlink" title=""></a></h6><h6 id="查："><a href="#查：" class="headerlink" title="查："></a>查：</h6><ul>
<li>chatAt()、charCodeAt()</li>
<li>indexOf()</li>
<li>startsWith()、endsWith()</li>
<li>includes()</li>
<li>search()</li>
</ul>
<p>（1）indexOf( )/lastIndexOf( ) :获取字符串中指定内容的索引</p>
<p><strong>语法 1</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">索引值 = str.<span class="title function_">indexOf</span>(想要查询的字符串);</span><br></pre></td></tr></table></figure>

<p>备注：<code>indexOf()</code> 是从前向后查找字符串的位置。同理，<code>lastIndexOf()</code>是从后向前寻找。</p>
<p><strong>解释</strong>：可以检索一个字符串中是否含有指定内容。如果字符串中含有该内容，则会返回其<strong>第一次出现</strong>的索引；如果没有找到指定的内容，则返回 -1。</p>
<p>因此可以得出一个重要技巧：</p>
<ul>
<li><p>  <strong>如果获取的索引值为 0，说明字符串是以查询的参数为开头的</strong>。</p>
</li>
<li><p>  如果获取的索引值为-1，说明这个字符串中没有指定的内容。</p>
</li>
</ul>
<p>举例 1：(查找单个字符)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> str = <span class="string">&#x27;abcdea&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//给字符查索引(索引值为0,说明字符串以查询的参数为开头)</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">indexOf</span>(<span class="string">&#x27;c&#x27;</span>));<span class="comment">//2</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;c&#x27;</span>));<span class="comment">//2</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">indexOf</span>(<span class="string">&#x27;a&#x27;</span>));<span class="comment">//0</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;a&#x27;</span>))<span class="comment">//5</span></span><br></pre></td></tr></table></figure>

<p>举例 2：（查找字符串）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name=<span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">indexOf</span>(<span class="string">&#x27;fei&#x27;</span>));<span class="comment">//4</span></span><br></pre></td></tr></table></figure>

<p><strong>语法 2</strong>：</p>
<p>这个方法还可以指定第二个参数，用来指定查找的<strong>起始位置</strong>。语法如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">索引值 = str.<span class="title function_">indexOf</span>(想要查询的字符串, [起始位置]);</span><br></pre></td></tr></table></figure>

<p>举例 3：（两个参数时，需要特别注意）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name=<span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"><span class="keyword">let</span> result=name.<span class="title function_">indexOf</span>(<span class="string">&#x27;i&#x27;</span>,<span class="number">2</span>);<span class="comment">// 从第二个位置开始查找 &#x27;i&#x27;这个字符 </span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result);<span class="comment">//6</span></span><br></pre></td></tr></table></figure>

<p><strong>案例</strong>：查找字符串”xiaofeixiaPan”中，所有 <code>a</code> 出现的位置以及次数。</p>
<p>思路：</p>
<p>​        （1）先查找第一个 a 出现的位置。</p>
<p>​        （2）只要 indexOf 返回的结果不是 -1 就继续往后查找。</p>
<p>​        （3）因为 indexOf 只能查找到第一个，所以后面的查找，可以利用第二个参数，在当前索引加 1，从而继续查找。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name=<span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> index=name.<span class="title function_">indexOf</span>(<span class="string">&#x27;i&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> num=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(index!=-<span class="number">1</span>)&#123;</span><br><span class="line">     <span class="variable language_">console</span>.<span class="title function_">log</span>(index);</span><br><span class="line">     num++;</span><br><span class="line">     index=name.<span class="title function_">indexOf</span>(<span class="string">&#x27;i&#x27;</span>,index+<span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;i 出现的次数是：&#x27;</span>+num);</span><br></pre></td></tr></table></figure>

<p>打印结果为：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">8</span></span><br><span class="line">i 出现的次数是：<span class="number">3</span></span><br></pre></td></tr></table></figure>



<p>(2).search( ):获取字符串中指定内容的索引（参数里一般是正则）</p>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">索引值 = str.search(想要查找的字符串);</span><br><span class="line">索引值 = str.search(正则表达式);</span><br></pre></td></tr></table></figure>

<p><code>search()</code> 方法里的参数，既可以传字符串，也可以传正则表达式。</p>
<p><strong>解释</strong>：可以检索一个字符串中是否含有指定内容。如果字符串中含有该内容，则会返回其<strong>第一次出现</strong>的索引；如果没有找到指定的内容，则返回 -1。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">search</span>(<span class="string">&#x27;Pan&#x27;</span>)); <span class="comment">// 打印结果：10</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">search</span>(<span class="regexp">/Pan/i</span>)); <span class="comment">// 打印结果：10</span></span><br></pre></td></tr></table></figure>

<p>备注：上方的<code>/yi/i</code>采用的是正则表达式的写法，意思是，让 name去匹配字符<code>Pan</code>，忽略大小写。</p>
<p>(3).includes( ):字符串中是否包含指定的内容</p>
<p>语法：</p>
<p>布尔值 = str.includes(想要查找的字符串, [position]);</p>
<p><strong>解释</strong>：判断一个字符串中是否含有指定内容。如果字符串中含有该内容，则会返回 true；否则返回 false。</p>
<p>参数中的 <code>position</code>：如果不指定，则默认为0；如果指定，则规定了检索的起始位置。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const name = &#x27;xiaofeixiaPan&#x27;;</span><br><span class="line"></span><br><span class="line">console.log(name.includes(&#x27;fei&#x27;)); // 打印结果：true</span><br><span class="line">console.log(name.includes(&#x27;haha&#x27;)); // 打印结果：false</span><br><span class="line"></span><br><span class="line">console.log(name.includes(&#x27;yi&#x27;,7)); // 打印结果：false</span><br></pre></td></tr></table></figure>

<p>(4).startsWith( ):字符串是否以指定的内容开头</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">布尔值 = str.<span class="title function_">startsWith</span>(想要查找的内容, [position]);</span><br></pre></td></tr></table></figure>

<p><strong>解释</strong>：判断一个字符串是否以指定的子字符串开头。如果是，则返回 true；否则返回 false。</p>
<p><strong>参数中的position</strong>：</p>
<ul>
<li><p>如果不指定，则默认为0。</p>
</li>
<li><p>如果指定，则规定了<strong>检索的起始位置</strong>。检索的范围包括：这个指定位置开始，直到字符串的末尾。即：[position, str.length)</p>
</li>
</ul>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;abcdefg&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;a&#x27;</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;b&#x27;</span>)); <span class="comment">// 打印结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 因为指定了起始位置为3，所以是在 defg 这个字符串中检索。</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;d&#x27;</span>,<span class="number">3</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;c&#x27;</span>,<span class="number">3</span>)); <span class="comment">// 打印结果：false</span></span><br></pre></td></tr></table></figure>

<p>(5).endsWith()：字符串是否以指定的内容结尾</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">布尔值 = str.<span class="title function_">endsWith</span>(想要查找的内容, [position]);</span><br></pre></td></tr></table></figure>

<p><strong>解释</strong>：判断一个字符串是否以指定的子字符串结尾。如果是，则返回 true；否则返回 false。</p>
<p><strong>参数中的position</strong>：</p>
<ul>
<li><p>如果不指定，则默认为 str.length。</p>
</li>
<li><p>如果指定，则规定了<strong>检索的结束位置</strong>。检索的范围包括：从第一个字符串开始，直到这个指定的位置。即：[0, position)</p>
</li>
<li><p>或者你可以这样简单理解：endsWith() 方法里的position，表示<strong>检索的长度</strong>。</p>
</li>
</ul>
<p>注意：startsWith() 和 endsWith()这两个方法，他们的 position 的含义是不同的。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;abcdefg&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;g&#x27;</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;f&#x27;</span>)); <span class="comment">// 打印结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 因为指定了截止位置为3，所以是在 abc 这个长度为3字符串中检索</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;c&#x27;</span>, <span class="number">3</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;d&#x27;</span>, <span class="number">3</span>)); <span class="comment">// 打印结果：false</span></span><br></pre></td></tr></table></figure>

<p>(6).获取指定位置的字符</p>
<p>​        （1）charAt(index)</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符 = str.<span class="title function_">charAt</span>(index);</span><br></pre></td></tr></table></figure>

<p>解释：返回字符串指定位置的字符。这里的 <code>str.charAt(index)</code>和<code>str[index]</code>的效果是一样的。</p>
<p>注意：字符串中第一个字符的下标是 0。如果参数 index 不在 [0, string.length) 之间，该方法将返回一个空字符串。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">charAt</span>(<span class="number">3</span>)); <span class="comment">// 打印结果：o</span></span><br></pre></td></tr></table></figure>

<p>​            (2)  str[index]</p>
<p><code>str.charAt(index)</code>和<code>str[index]</code>的效果是一样的，不再赘述。区别在于：<code>str[index]</code>是 H5 标准里新增的特性。</p>
<p>​            (3)  charCodeAt(index)</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符 = str.<span class="title function_">charCodeAt</span>(index);</span><br></pre></td></tr></table></figure>

<p>解释：返回字符串指定位置的字符的 Unicode 编码。不会修改原字符串。</p>
<p>在实际应用中，通过这个方法，我们可以判断用户按下了哪个按键。</p>
<p><strong>代码举例</strong>：打印字符串的<strong>占位长度</strong>。</p>
<p>提示：一个英文占一个位置，一个中文占两个位置。</p>
<p>思路：判断该字符是否在 0-127 之间（在的话是英文，不在是非英文）。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;xiaofeixiaPan&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> a=name.<span class="title function_">charCodeAt</span>(<span class="number">3</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);<span class="comment">//111</span></span><br></pre></td></tr></table></figure>

<h6 id="-1"><a href="#-1" class="headerlink" title=""></a></h6><h4 id="转换方法"><a href="#转换方法" class="headerlink" title="转换方法"></a>转换方法</h4><h6 id="split-：字符串转换为数组-【重要】"><a href="#split-：字符串转换为数组-【重要】" class="headerlink" title="split()：字符串转换为数组 【重要】"></a>split()：字符串转换为数组 【重要】</h6><p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新的数组 = str.<span class="title function_">split</span>(分隔符);</span><br></pre></td></tr></table></figure>

<p>解释：通过指定的分隔符，将一个字符串拆分成一个<strong>数组</strong>。不会改变原字符串。</p>
<p>备注：<code>split()</code>这个方法在实际开发中用得非常多。一般来说，从接口拿到的 json 数据中，经常会收到类似于<code>&quot;q, i, a, n&quot;</code>这样的字符串，前端需要将这个字符串拆分成<code>[&#39;q&#39;, &#39;i&#39;, &#39;a&#39;, &#39;n&#39;]</code>数组，这个时候<code>split()</code>方法就派上用场了。</p>
<p><strong>代码举例 1</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">&#x27;xiao,fei,xia,Pan&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> arr=str.<span class="title function_">split</span>(<span class="string">&#x27;,&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr);<span class="comment">//(4) [&#x27;xiao&#x27;, &#x27;fei&#x27;, &#x27;xia&#x27;, &#x27;Pan&#x27;]</span></span><br></pre></td></tr></table></figure>



<h3 id="八-与"><a href="#八-与" class="headerlink" title="八.===与=="></a>八.===与==</h3><h5 id="I"><a href="#I" class="headerlink" title="I.=="></a>I.==</h5><p><code>==</code>和<code>！=  </code>都会先进行类型转换，再确定操作数是否相等</p>
<p>如果任一操作数是布尔值，则将其转换为数值再比较是否相等</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result1 = (<span class="literal">true</span> == <span class="number">1</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>如果一个操作数是字符串，另一个操作数是数值，则尝试将字符串转换为数值，再比较是否相等</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result1 = (<span class="string">&quot;55&quot;</span> == <span class="number">55</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>如果一个操作数是对象，另一个操作数不是，则调用对象的 <code>valueOf()</code>方法取得其原始值，再根据前面的规则进行比较</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;<span class="attr">valueOf</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;<span class="keyword">return</span> <span class="number">1</span>&#125;&#125;</span><br><span class="line"><span class="keyword">let</span> result1 = (obj == <span class="number">1</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p><code>null</code>和<code>undefined</code>相等</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result1 = (<span class="literal">null</span> == <span class="literal">undefined</span> ); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>如果有任一操作数是 <code>NaN</code> ，则相等操作符返回 <code>false</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result1 = (<span class="title class_">NaN</span> == <span class="title class_">NaN</span> ); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>如果两个操作数都是对象，则比较它们是不是同一个对象。如果两个操作数都指向同一个对象，则相等操作符返回<code>true</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj1 = &#123;<span class="attr">name</span>:<span class="string">&quot;xxx&quot;</span>&#125;</span><br><span class="line"><span class="keyword">let</span> obj2 = &#123;<span class="attr">name</span>:<span class="string">&quot;xxx&quot;</span>&#125;</span><br><span class="line"><span class="keyword">let</span> result1 = (obj1 == obj2 ); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>总结：</p>
<ul>
<li>两个都为简单类型，字符串和布尔值都会转换成数值，再比较</li>
<li>简单类型与引用类型比较，对象转化成其原始类型的值，再比较</li>
<li>两个都为引用类型，则比较它们是否指向同一个对象</li>
<li>null 和 undefined 相等</li>
<li>存在 NaN 则返回 false</li>
</ul>
<h5 id="II"><a href="#II" class="headerlink" title="II. ==="></a>II. ===</h5><p>全等操作符由 3 个等于号（ === ）表示，只有两个操作数在不转换的前提下相等才返回 <code>true</code>。即类型相同，值也需相同</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result1 = (<span class="string">&quot;55&quot;</span> === <span class="number">55</span>); <span class="comment">// false，不相等，因为数据类型不同</span></span><br><span class="line"><span class="keyword">let</span> result2 = (<span class="number">55</span> === <span class="number">55</span>); <span class="comment">// true，相等，因为数据类型相同值也相同</span></span><br></pre></td></tr></table></figure>

<p><code>undefined</code> 和 <code>null</code> 与自身严格相等</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result1 = (<span class="literal">null</span> === <span class="literal">null</span>)  <span class="comment">//true</span></span><br><span class="line"><span class="keyword">let</span> result2 = (<span class="literal">undefined</span> === <span class="literal">undefined</span>)  <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p>虽然null == undefined 是true（因为这两个值类似），但null === undefined 是<br>false，因为它们不是相同的数据类型。</p>
<p>总结：</p>
<p><strong>除了在比较对象属性为<code>null</code>或者<code>undefined</code>的情况下，我们可以使用相等操作符（==），其他情况建议一律使用全等操作符（===）</strong></p>
<h3 id="九-typeof和instanceof"><a href="#九-typeof和instanceof" class="headerlink" title="九.typeof和instanceof"></a>九.typeof和instanceof</h3><h5 id="typeof"><a href="#typeof" class="headerlink" title="typeof"></a>typeof</h5><blockquote>
<p><strong><code>typeof</code></strong> 操作符返回一个<code>字符串</code>，表示未经计算的操作数的类型。</p>
<ul>
<li>可以判断: undefined/ 数值 / 字符串 / 布尔值 / function</li>
<li>不能判断: null与object  object与array</li>
<li><code>注意</code>: 运行<code>console.log(typeof undefined)</code>时,得到的的也是一个<code>字符串,同时为小写!!</code>–&gt; <code>&#39;undefined&#39;</code></li>
</ul>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// typeof返回数据类型的字符串表达</span></span><br><span class="line">   <span class="keyword">var</span> a</span><br><span class="line"></span><br><span class="line">   <span class="comment">//注意:typeof返回的是字符串</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(a, <span class="keyword">typeof</span> a, <span class="keyword">typeof</span> a===<span class="string">&#x27;undefined&#x27;</span>,a===<span class="literal">undefined</span> )  </span><br><span class="line"><span class="comment">// undefined &#x27;undefined&#x27; true true</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span> === <span class="string">&#x27;undefined&#x27;</span>) <span class="comment">//false</span></span><br><span class="line">   a = <span class="number">4</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a===<span class="string">&#x27;number&#x27;</span>) <span class="comment">//true</span></span><br><span class="line">   a = <span class="string">&#x27;xiaofeixia&#x27;</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a===<span class="string">&#x27;string&#x27;</span>) <span class="comment">//true</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a===<span class="string">&#x27;String&#x27;</span>) <span class="comment">//false  --&gt;注意,返回的类型为小写</span></span><br><span class="line">   a = <span class="literal">true</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a===<span class="string">&#x27;boolean&#x27;</span>) <span class="comment">//true</span></span><br><span class="line">   a = <span class="literal">null</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a, a===<span class="literal">null</span>) <span class="comment">// &#x27;object&#x27;  true</span></span><br><span class="line">  <span class="keyword">let</span> b=&#123;&#125;</span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> b,<span class="keyword">typeof</span> <span class="literal">null</span>) </span><br><span class="line"><span class="comment">// &#x27;object&#x27; &#x27;object&#x27;  --&gt;所以Typeof不能判断null与object</span></span><br></pre></td></tr></table></figure>

<p>typeof null<code>为</code>object</p>
<p>如果你需要在 <code>if</code> 语句中判断是否为 <code>null</code>，直接通过<code>===null</code>来判断就好</p>
<p>引用类型数据，用<code>typeof</code>来判断的话，除了<code>function</code>会被识别出来之外，其余的都输出<code>object</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span> <span class="number">1</span> <span class="comment">// &#x27;number&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="string">&#x27;1&#x27;</span> <span class="comment">// &#x27;string&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="literal">undefined</span> <span class="comment">// &#x27;undefined&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="literal">true</span> <span class="comment">// &#x27;boolean&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="title class_">Symbol</span>() <span class="comment">// &#x27;symbol&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="literal">null</span> <span class="comment">// &#x27;object&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> [] <span class="comment">// &#x27;object&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> &#123;&#125; <span class="comment">// &#x27;object&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="variable language_">console</span> <span class="comment">// &#x27;object&#x27;</span></span><br><span class="line"><span class="keyword">typeof</span> <span class="variable language_">console</span>.<span class="property">log</span> <span class="comment">// &#x27;function&#x27;</span></span><br></pre></td></tr></table></figure>

<h5 id="instanceof"><a href="#instanceof" class="headerlink" title="instanceof"></a>instanceof</h5><p><strong><code>instanceof</code></strong> <strong>运算符</strong>用于检测构造函数的 <code>prototype</code> 属性是否出现在某个实例对象的原型链上。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> b1 = &#123;</span><br><span class="line">    <span class="attr">b2</span>: [<span class="number">1</span>, <span class="string">&#x27;abc&#x27;</span>, <span class="variable language_">console</span>.<span class="property">log</span>],</span><br><span class="line">    <span class="attr">b3</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">      <span class="keyword">return</span>  <span class="function">() =&gt;</span>&#123;  <span class="keyword">return</span>   <span class="string">&#x27;hello&#x27;</span>&#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"> <span class="comment">/**使用instanceof进行对象判断*/</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(b1 <span class="keyword">instanceof</span> <span class="title class_">Object</span>, b1 <span class="keyword">instanceof</span> <span class="title class_">Array</span>) <span class="comment">// true  false</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(b1.<span class="property">b2</span> <span class="keyword">instanceof</span> <span class="title class_">Array</span>, b1.<span class="property">b2</span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>) <span class="comment">// true true</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(b1.<span class="property">b3</span> <span class="keyword">instanceof</span> <span class="title class_">Function</span>, b1.<span class="property">b3</span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>) </span><br><span class="line"><span class="comment">// true true</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">/**使用typeof进行对象中某属性的判断*/</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> b1.<span class="property">b2</span>, <span class="keyword">typeof</span> <span class="literal">null</span>) <span class="comment">// &#x27;object&#x27; &#x27;object&#x27;  </span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> b1.<span class="property">b3</span>===<span class="string">&#x27;function&#x27;</span>) <span class="comment">// true</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> b1.<span class="property">b2</span>[<span class="number">2</span>]===<span class="string">&#x27;function&#x27;</span>) <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">/**调用对象与数组中某函数示例*/</span></span><br><span class="line">  b1.<span class="property">b2</span>[<span class="number">2</span>](<span class="string">&#x27;调用console.log打印hello&#x27;</span>)    <span class="comment">//调用console.log打印hello</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(b1.<span class="title function_">b3</span>()()) <span class="comment">// hello</span></span><br></pre></td></tr></table></figure>



<p>构造函数通过<code>new</code>可以实例对象，<code>instanceof</code>能判断这个对象是否是之前那个构造函数生成的对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义构建函数</span></span><br><span class="line"><span class="keyword">let</span> <span class="title class_">Car</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line"><span class="keyword">let</span> benz = <span class="keyword">new</span> <span class="title class_">Car</span>()</span><br><span class="line">benz <span class="keyword">instanceof</span> <span class="title class_">Car</span> <span class="comment">// true</span></span><br><span class="line"><span class="keyword">let</span> car = <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&#x27;xxx&#x27;</span>)</span><br><span class="line">car <span class="keyword">instanceof</span> <span class="title class_">String</span> <span class="comment">// true</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;xxx&#x27;</span></span><br><span class="line">str <span class="keyword">instanceof</span> <span class="title class_">String</span> <span class="comment">// false</span></span><br></pre></td></tr></table></figure>



<p><code>typeof</code>与<code>instanceof</code>都是判断数据类型的方法，区别如下：</p>
<ul>
<li><p><code>typeof</code>会返回一个变量的基本类型（是一个字符串），<code>instanceof</code>返回的是一个布尔值</p>
</li>
<li><p><code>instanceof</code> 可以准确地判断复杂引用数据类型，但是不能正确判断基础数据类型</p>
</li>
<li><p>而<code>typeof</code> 也存在弊端，它虽然可以判断基础数据类型（<code>null</code> 除外），但是引用数据类型中，除了<code>function</code> 类型以外，其他的也无法判断</p>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">潘静怡</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/">https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://mengqiuleo.gitee.io" target="_blank">小飞侠Pan的Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JS/">JS</a><a class="post-meta__tags" href="/tags/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/">数据类型</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/26/js/%E6%95%B0%E7%BB%84/"><img class="prev-cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">JS基本语法之数组</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/"><img class="next-cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JS基本语法之call/bind/apply</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">原型与原型链</div></div></a></div><div><a href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基本语法之call/bind/apply</div></div></a></div><div><a href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基本语法之数组</div></div></a></div><div><a href="/2022/02/26/js/%E7%BB%84%E5%90%88(Compose)%E5%87%BD%E6%95%B0/" title="组合（Compose）函数"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">组合（Compose）函数</div></div></a></div><div><a href="/2022/02/26/js/%E4%BD%9C%E7%94%A8%E5%9F%9F%E4%B8%8E%E5%8F%98%E9%87%8F%E6%8F%90%E5%8D%87/" title="作用域与变量提升"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">作用域与变量提升</div></div></a></div><div><a href="/2022/02/26/js%E5%BC%82%E6%AD%A5/%E5%BC%82%E5%B8%B8error/" title="JS中的异常error处理"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS中的异常error处理</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">潘静怡</div><div class="author-info__description">一路春光, 一路荆棘, 不虚此行</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_52834435?type=blog"><i class="iconfont icon-csdn1"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://blog.csdn.net/weixin_52834435?spm=1000.2115.3001.5343" target="_blank" title="CSDN"><i class="iconfont icon-csdn1"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo" target="_blank" title="Gitee"><i class="iconfont icon-gitee2"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130203.jpg" target="_blank" title="QQ"><i class="fa-brands fa-qq"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130201.jpg" target="_blank" title="WeChat"><i class="fa-brands fa-weixin"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80-JS-%E4%B8%AD%E4%B8%80%E5%85%B1%E6%9C%89%E5%85%AB%E7%A7%8D%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.</span> <span class="toc-text">一.JS 中一共有八种数据类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C-%E6%A0%88%E5%86%85%E5%AD%98%E5%92%8C%E5%A0%86%E5%86%85%E5%AD%98"><span class="toc-number">2.</span> <span class="toc-text">二.栈内存和堆内存</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9ANull-%E5%92%8C-Undefined"><span class="toc-number">3.</span> <span class="toc-text">三.基本数据类型：Null 和 Undefined</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Null%EF%BC%9A%E7%A9%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.1.</span> <span class="toc-text">Null：空对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#undefined%EF%BC%9A%E6%9C%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.</span> <span class="toc-text">undefined：未定义类型</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#case1%EF%BC%9A%E5%8F%98%E9%87%8F%E5%B7%B2%E5%A3%B0%E6%98%8E%EF%BC%8C%E6%9C%AA%E8%B5%8B%E5%80%BC%E6%97%B6"><span class="toc-number">3.2.0.1.</span> <span class="toc-text">case1：变量已声明，未赋值时</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#case2%EF%BC%9A%E5%8F%98%E9%87%8F%E6%9C%AA%E5%A3%B0%E6%98%8E%EF%BC%88%E6%9C%AA%E5%AE%9A%E4%B9%89%EF%BC%89%E6%97%B6"><span class="toc-number">3.2.0.2.</span> <span class="toc-text">case2：变量未声明（未定义）时</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#case3%EF%BC%9A%E5%87%BD%E6%95%B0%E6%97%A0%E8%BF%94%E5%9B%9E%E5%80%BC%E6%97%B6"><span class="toc-number">3.2.0.3.</span> <span class="toc-text">case3：函数无返回值时</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#case4%EF%BC%9A%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0%E6%97%B6%EF%BC%8C%E6%9C%AA%E4%BC%A0%E5%8F%82"><span class="toc-number">3.2.0.4.</span> <span class="toc-text">case4：调用函数时，未传参</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Null-%E5%92%8C-Undefined%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">3.3.</span> <span class="toc-text">Null 和 Undefined的区别</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9ANumber"><span class="toc-number">4.</span> <span class="toc-text">四.基本数据类型：Number</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E8%8C%83%E5%9B%B4"><span class="toc-number">4.1.</span> <span class="toc-text">数值范围</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#NaN"><span class="toc-number">4.2.</span> <span class="toc-text">NaN</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">4.2.0.1.</span> <span class="toc-text">类型转换</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%88%8D%E5%85%A5%E6%93%8D%E4%BD%9C"><span class="toc-number">4.2.0.2.</span> <span class="toc-text">舍入操作</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%EF%BC%88%E9%9A%90%E5%BC%8F%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%89%EF%BC%9A"><span class="toc-number">4.2.0.3.</span> <span class="toc-text">方式（隐式的类型转换）：</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%94-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9ABoolean"><span class="toc-number">5.</span> <span class="toc-text">五.基本数据类型：Boolean</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2"><span class="toc-number">5.0.0.1.</span> <span class="toc-text">隐式转换</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%98%BE%E5%BC%8F%E8%BD%AC%E6%8D%A2"><span class="toc-number">5.0.0.2.</span> <span class="toc-text">显式转换</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AD-0-%E2%80%9C%E2%80%9D-null-false-undefined%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">6.</span> <span class="toc-text">六.0, “”, null, false, undefined的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%83-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9AString"><span class="toc-number">7.</span> <span class="toc-text">七.基本数据类型：String</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E4%B8%BAString"><span class="toc-number">7.1.</span> <span class="toc-text">转换为String</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%88%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%89%EF%BC%9A"><span class="toc-number">7.1.0.1.</span> <span class="toc-text">方式一（强制类型转换）：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%88%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%89%EF%BC%9A"><span class="toc-number">7.1.0.2.</span> <span class="toc-text">方式二（强制类型转换）：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%88%E9%9A%90%E5%BC%8F%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%89"><span class="toc-number">7.2.</span> <span class="toc-text">方式三（隐式的类型转换）:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">7.3.</span> <span class="toc-text">字符串的常用方法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%A2%9E%EF%BC%9A"><span class="toc-number">7.3.0.1.</span> <span class="toc-text">增：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%88%A0%EF%BC%9A"><span class="toc-number">7.3.0.2.</span> <span class="toc-text">删：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%94%B9%EF%BC%9A"><span class="toc-number">7.3.0.3.</span> <span class="toc-text">改：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link"><span class="toc-number">7.3.0.4.</span> <span class="toc-text"></span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%9F%A5%EF%BC%9A"><span class="toc-number">7.3.0.5.</span> <span class="toc-text">查：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#-1"><span class="toc-number">7.3.0.6.</span> <span class="toc-text"></span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E6%96%B9%E6%B3%95"><span class="toc-number">7.4.</span> <span class="toc-text">转换方法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#split-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E7%BB%84-%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-number">7.4.0.1.</span> <span class="toc-text">split()：字符串转换为数组 【重要】</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AB-%E4%B8%8E"><span class="toc-number">8.</span> <span class="toc-text">八.&#x3D;&#x3D;&#x3D;与&#x3D;&#x3D;</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#I"><span class="toc-number">8.0.1.</span> <span class="toc-text">I.&#x3D;&#x3D;</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#II"><span class="toc-number">8.0.2.</span> <span class="toc-text">II. &#x3D;&#x3D;&#x3D;</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B9%9D-typeof%E5%92%8Cinstanceof"><span class="toc-number">9.</span> <span class="toc-text">九.typeof和instanceof</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#typeof"><span class="toc-number">9.0.1.</span> <span class="toc-text">typeof</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#instanceof"><span class="toc-number">9.0.2.</span> <span class="toc-text">instanceof</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/ajax/ajax/" title="JS异步之ajax"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS异步之ajax"/></a><div class="content"><a class="title" href="/2022/02/26/ajax/ajax/" title="JS异步之ajax">JS异步之ajax</a><time datetime="2022-02-26T10:53:56.362Z" title="发表于 2022-02-26 18:53:56">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基本语法之call/bind/apply"/></a><div class="content"><a class="title" href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply">JS基本语法之call/bind/apply</a><time datetime="2022-02-26T10:52:29.044Z" title="发表于 2022-02-26 18:52:29">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基础之数据类型及其判断"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断">JS基础之数据类型及其判断</a><time datetime="2022-02-26T10:49:21.438Z" title="发表于 2022-02-26 18:49:21">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基本语法之数组"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组">JS基本语法之数组</a><time datetime="2022-02-26T10:47:39.357Z" title="发表于 2022-02-26 18:47:39">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="原型与原型链"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链">原型与原型链</a><time datetime="2022-02-26T10:42:32.957Z" title="发表于 2022-02-26 18:42:32">2022-02-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By 潘静怡</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">welcome to my blog!🤪</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="/js/mobile.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start -->
  <script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/hexo-github-calendar@1.21/hexo_githubcalendar.js"></script>
  <script data-pjax>
        function GithubCalendarConfig(){
            var git_githubapiurl ="https://python-gitee-calendar-api.vercel.app/api?mengqiuleo";
            var git_color =['#ebedf0', '#f0fff4', '#dcffe4', '#bef5cb', '#85e89d', '#34d058', '#28a745', '#22863a', '#176f2c', '#165c26', '#144620'];
            var git_user ="mengqiuleo";
            var parent_div_git = document.getElementById('recent-posts');
            var git_div_html = '<div class="recent-post-item" style="width:100%;height:auto;padding:10px;"><div id="github_loading" style="width:10%;height:100%;margin:0 auto;display: block"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  viewBox="0 0 50 50" style="enable-background:new 0 0 50 50" xml:space="preserve"><path fill="#d0d0d0" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" transform="rotate(275.098 25 25)"><animateTransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.6s" repeatCount="indefinite"></animateTransform></path></svg></div><div id="github_container"></div></div>';
            if(parent_div_git && location.pathname =='/'){
                console.log('已挂载github calendar')
                // parent_div_git.innerHTML=git_div_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",git_div_html) // 有报错，但不影响使用(支持pjax跳转)
            };
            GithubCalendar(git_githubapiurl,git_color,git_user)
        }
        if(document.getElementById('recent-posts')){
            GithubCalendarConfig()
        }
    </script>
    <style>#github_container{min-height:280px}@media screen and (max-width:650px) {#github_container{background-image:;min-height:0px}}</style>
    <style></style><!-- hexo injector body_end end --></body></html>